/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.serialportmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.serialportmodule.data.SerialPortModuleDataObject;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.utils.StringVerification;

/**
 *
 * @author root
 */
public class SerialPortModuleSendCommandAction extends AtofModuleAction {

    @Override
    public boolean operateRequest(AtofModuleService request) {


        SerialPortModuleProviderService service =
                (SerialPortModuleProviderService) request;
        SerialPortModuleDataObject dataObj =
                (SerialPortModuleDataObject) service.getDataObj();
        try {

            LogSenderService.putMessageOnQueue(
                    "SerialPortModuleStartConnectionAction",
                    AtofLogLevel.INFO,
                    "Sending (" + dataObj.getCommand() + ") command to  serial connection from active port... ",
                    dataObj.getMessageId());
            if (service.m_serialConnection == null) {
                LogSenderService.putMessageOnQueue(
                        "SerialPortModuleStartConnectionAction",
                        AtofLogLevel.INFO,
                        "No active serial connection found. Can not send command... ",
                        dataObj.getMessageId());
                return false;
            }
            String output = service.m_serialConnection.sendCommand(dataObj.getCommand());
            String verify = "";
            if (dataObj.getVerifyString() != null) {
                verify = dataObj.getVerifyString().trim();
            }
            boolean result = false;
            if (!verify.equals("")) {
                result = StringVerification.verifyText(verify,
                        output);
            } else {
                result = true;
            }
            LogSenderService.putMessageOnQueue(
                    "SerialPortModuleStartConnectionAction",
                    AtofLogLevel.INFO,
                    "Command (" + dataObj.getCommand() + ")  sent to  serial connection  from active port... Output of command was:" + output,
                    dataObj.getMessageId());
            return result;

        } catch (Exception ex) {
            ex.printStackTrace();
            throw new TestStepException(TestStepResultType.FAILURE,
                    ex.getMessage());
        }
    }
}
